package com.hjwoo.app.views;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.hjwoo.app.R;
import com.hjwoo.app.utils.SharedPreferences;

import java.util.ArrayList;

public class WelcomeActivity extends Activity
{

    private static final String TAG            = "WelcomeActivity";
    private static final String FIRST_TIME_USE = "first-time-use";
    final                int    pageCount      = 4;
    ViewPager viewPager          = null;
    ImageView    imgcurDot          = null;
    LinearLayout layoutDotContainer = null;
    Button enterbt;
    int                  offset   = 0;
    int                  curPos   = 0;
    ArrayList<ImageView> viewlist = new ArrayList<>();
    FrameLayout frameLayout;

    public void showHome(Activity context)
    {
        Intent intent = new Intent(context, MainActivity.class);
        context.startActivity(intent);
        WelcomeActivity.this.finish();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
//        requestWindowFeature(Window.FEATURE_NO_TITLE); //设置无标题
//        getWindow().setFlags(WindowManager.LayoutParams.FILL_PARENT, WindowManager.LayoutParams.FILL_PARENT);  //设置全屏
        setContentView(R.layout.activity_welcome);
        //获取是不是第一次进入
        boolean firstTimeUse = SharedPreferences.getInstance().getBoolean(FIRST_TIME_USE, true);
        if (firstTimeUse)
        {
            initGuideGallery();
        }
        else
        {
            initLaunchLogo();
        }
    }

    //显示欢迎图片
    private void initLaunchLogo()
    {
        findViewById(R.id.frameLayout).setVisibility(View.GONE);
        new Handler().postDelayed(new Runnable()
        {
            @Override
            public void run()
            {
                showHome(WelcomeActivity.this);
            }
        }, 500);
    }

    //初始化控件
    void initGuideGallery()
    {

        RelativeLayout welcome = (RelativeLayout) findViewById(R.id.welcome);
        welcome.setBackgroundColor(getResources().getColor(R.color.ui_base_color));

        final Animation fadeIn = AnimationUtils.loadAnimation(this, R.anim.fadein);

        imgcurDot = (ImageView) findViewById(R.id.imgWelcomeDot);
        layoutDotContainer = (LinearLayout) findViewById(R.id.layoutDotContainer);

        layoutDotContainer.setVisibility(View.VISIBLE);

        offset = imgcurDot.getDrawable().getBounds().width();
        enterbt = (Button) findViewById(R.id.enterbt);

        enterbt.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                //标记为不是第一次进入
                SharedPreferences.getInstance().putBoolean(FIRST_TIME_USE, false);
                //跳转到登录界面
                Intent intent = new Intent(WelcomeActivity.this, MainActivity.class);
                WelcomeActivity.this.startActivity(intent);
                WelcomeActivity.this.finish();
            }
        });

        viewPager = (ViewPager) findViewById(R.id.welcomeViewPager);
        for (int i = 0; i < pageCount; i++)
        {
            ImageView img = new ImageView(this);
            switch (i)
            {
            case 0:
                img.setImageResource(R.drawable.welcome1);
                break;
            case 1:
                img.setImageResource(R.drawable.welcome2);
                break;
            case 2:
                img.setImageResource(R.drawable.welcome3);
                break;
            default:
                img.setImageResource(R.drawable.welcome4);
                break;
            }

            ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.FILL_PARENT,
                ViewGroup.LayoutParams.FILL_PARENT);
            img.setLayoutParams(params);
            img.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
            viewlist.add(img);
        }
        initDotViews();
        WelcomePagerAdapter welcomePagerAdapter = new WelcomePagerAdapter();
        viewPager.setAdapter(welcomePagerAdapter);
        viewPager.clearAnimation();
        ;
        viewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener()
        {
            @Override
            public void onPageSelected(int position)
            {
                int pos = position % viewlist.size();
                if (position == viewlist.size() - 1)
                {
//                    显示进入按钮
                    enterbt.setVisibility(View.VISIBLE);
                    enterbt.startAnimation(fadeIn);
                }
                else
                {
//                    隐藏
                    enterbt.setVisibility(View.GONE);
                }

                moveCursorTo(pos);
                curPos = pos;
                super.onPageSelected(position);
            }
        });
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public void initDotViews()
    {
        imgcurDot.getViewTreeObserver().addOnPreDrawListener(
            new ViewTreeObserver.OnPreDrawListener()
            {
                public boolean onPreDraw()
                {
                    // 获取ImageView的宽度也就是点图片的宽度
                    offset = imgcurDot.getWidth();
                    return true;
                }
            });

        for (int i = 0; i < viewlist.size(); i++)
        {
            ImageView imgdot = new ImageView(this);
            imgdot.setImageResource(R.drawable.dot1_w);
            imgdot.setLayoutParams(new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT, 1.0f));
            layoutDotContainer.addView(imgdot);
        }
    }

    private void moveCursorTo(int position)
    {
        AnimationSet animationSet = new AnimationSet(true);
        TranslateAnimation tAnim =
            new TranslateAnimation(offset * curPos, offset * position, 0, 0);
        animationSet.addAnimation(tAnim);
        animationSet.setDuration(300);
        animationSet.setFillAfter(true);
        imgcurDot.startAnimation(animationSet);
    }

    public class WelcomePagerAdapter extends PagerAdapter
    {
        public WelcomePagerAdapter()
        {
            super();
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object)
        {
            ((ViewPager) container).removeView(viewlist.get(position));
        }

        @Override
        public int getCount()
        {
            return viewlist.size();
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position)
        {
            ((ViewPager) container).addView(viewlist.get(position));
            return viewlist.get(position);
        }

        @Override
        public CharSequence getPageTitle(int position)
        {
            return String.valueOf(position);
        }

        @Override
        public boolean isViewFromObject(View view, Object object)
        {
            return view == object;
        }
    }
}
